package com.huluobo.a2201mvvm_imdemo.register;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.huluobo.a2201mvvm_imdemo.R;
import com.huluobo.a2201mvvm_imdemo.bean.UserBean;
import com.huluobo.a2201mvvm_imdemo.databinding.ActivityRegisterBinding;
import com.huluobo.a2201mvvm_imdemo.mvvm.BaseMVVMActivity;

public class RegisterActivity extends BaseMVVMActivity<ActivityRegisterBinding, RegisterViewModel> {

    @Override
    protected void initData() {
        viewModel.success.observe(this, userBean -> {
            Toast.makeText(this, "注册成功:" + userBean.getUsername(), Toast.LENGTH_SHORT).show();
            finish();
        });

        viewModel.failed.observe(this, s -> {
            Toast.makeText(this, "注册失败:" + s, Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    protected void initView() {
        dataBinding.registerToRegister.setOnClickListener(v -> {
            register();
        });
    }

    private void register() {
        String usernameString = dataBinding.registerUsername.getText().toString();
        String passwordString = dataBinding.registerPassword.getText().toString();
        String rePasswordString = dataBinding.registerRePassword.getText().toString();

        if (TextUtils.equals(passwordString, rePasswordString)) {
            viewModel.register(usernameString, passwordString);
        } else {
            Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected Class<RegisterViewModel> getViewModel() {
        return RegisterViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }
}